package com.supermap.util;

import com.alibaba.fastjson.JSONObject;
import com.supermap.tool.MysqlEntity;

import java.util.Set;

/**
 * @author xiaolu
 * @date 2018/6/19 下午2:36
 */
public class SQLFactory {

    /**
     * @param @param  mysqlentity
     * @param @return 设定文件
     * @return String    返回类型
     * @throws
     * @Title: getFastInsertSql
     * @Description: 拼接添加语句
     */
    public static String getFastInsertSql(MysqlEntity mysqlentity) {

        StringBuilder sql = new StringBuilder();

        String uvSbs = getIvSql(mysqlentity);

        sql.append("INSERT INTO ");
        sql.append(mysqlentity.getTableName());
        sql.append(uvSbs);

        return sql.toString();
    }

    /**
     * @param @param  mysqlentity
     * @param @return 设定文件
     * @return String    返回类型
     * @throws
     * @Title: getIvSql
     * @Description: 添加 条件值拼装
     */
    @SuppressWarnings("unchecked")
    private static String getIvSql(MysqlEntity mysqlentity) {

        StringBuilder vNSb = new StringBuilder();
        StringBuilder vSb = new StringBuilder();

        JSONObject uvjson = mysqlentity.getUpdateValue();

        Set<String> uvSet = uvjson.keySet();

        for (String uvv : uvSet) {
            vNSb.append(uvv + ",");
            vSb.append("'" + uvjson.get(uvv).toString().replace("'", "\"") + "',");
        }
        String vn = vNSb.toString().substring(0, vNSb.length() - 1);
        String v = vSb.toString().substring(0, vSb.length() - 1);

        return " (" + vn + ") VALUES (" + v + ")";
    }

    /**
     * @param @param  mysqlentity
     * @param @param  conditionValue
     * @param @param  updateValue
     * @param @return 设定文件
     * @return String    返回类型
     * @throws
     * @Title: getFastUpdateSql
     * @Description: 快捷修改 SQL封装
     */
    public static String getFastUpdateSql(MysqlEntity mysqlentity) {

        StringBuilder sql = new StringBuilder();

        String uvSbs = getUvSql(mysqlentity);
        String cvSbs = getCvSql(mysqlentity);

        sql.append("UPDATE ");
        sql.append(mysqlentity.getTableName());
        sql.append(" SET ");
        sql.append(uvSbs);
        sql.append(" where ");
        sql.append(cvSbs);

        return sql.toString();

    }

    /**
     * @param @param  mysqlentity
     * @param @return 设定文件
     * @return String    返回类型
     * @throws
     * @Title: getUvSql
     * @Description: 更新字段拼装
     */
    @SuppressWarnings("unchecked")
    private static String getUvSql(MysqlEntity mysqlentity) {

        StringBuilder uvSb = new StringBuilder();

        JSONObject uvjson = mysqlentity.getUpdateValue();

        Set<String> uvSet = uvjson.keySet();

        for (String uvv : uvSet) {
            uvSb.append(uvv);
            uvSb.append("='");
            uvSb.append(uvjson.get(uvv) + "',");
        }

        return uvSb.toString().substring(0, uvSb.length() - 1);
    }

    /**
     * @param @param  mysqlentity
     * @param @return 设定文件
     * @return String    返回类型
     * @throws
     * @Title: getCvSql
     * @Description: 查询条件拼装
     */
    @SuppressWarnings("unchecked")
    private static String getCvSql(MysqlEntity mysqlentity) {

        if (!StringFactory.isNotNull(mysqlentity.getConditionValue().toString())) {
            return null;
        }

        StringBuilder cvSb = new StringBuilder();

        JSONObject cvjson = mysqlentity.getConditionValue();

        Set<String> cvSet = cvjson.keySet();

        for (String cvv : cvSet) {
            cvSb.append(cvv);
            cvSb.append("='");
            cvSb.append(cvjson.get(cvv) + "' and ");
        }

        return cvSb.toString().substring(0, cvSb.length() - 4);
    }

    public static String getFastQuerySql(MysqlEntity mysqlentity) {
        StringBuilder sql = new StringBuilder();

        String cvSbs = getCvSql(mysqlentity);

        sql.append("select * from ");
        sql.append(mysqlentity.getTableName());
        if (mysqlentity.getConditionValue() != null) {
            sql.append(" where ");
            sql.append(cvSbs);
        }
        sql.append(" limit 0,100");

        return sql.toString();
    }
}
